package com.itheima.document.controller;

import com.itheima.common.vo.PageResult;
import com.itheima.common.vo.Result;
import com.itheima.document.dto.*;
import com.itheima.document.entity.FileHistory;
import com.itheima.document.service.DocumentService;
import com.itheima.sys.dto.CompanyUserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@RestController
public class DocumentController {


    @Autowired
    private DocumentService documentService;


    /**
     * 分页查询文档列表
     *
     * @param page     页码
     * @param pageSize 页大小
     * @param keyword  关键字
     * @return
     */
    @GetMapping("/document/pagingFile")
    public Result<PageResult<DocFileDTO>> queryFileList(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
            @RequestParam(value = "keyword", required = false) String keyword
    ) {
        return Result.success(documentService.queryFileList(page, pageSize, keyword));
    }


    /**
     * 解封/封禁 文档
     *
     * @param fileId
     * @return
     */
    @PutMapping("/document/{id}/{enable}")
    public Result updateEnable(@PathVariable("id") Long fileId, @PathVariable("enable") Boolean enable) {
        documentService.updateEnable(fileId, enable);
        return Result.success();
    }

    /**
     * 查询某个文档历史编辑记录
     *
     * @param fileId
     * @return
     */
    @GetMapping("/document/{id}/history")
    public Result<PageResult<FileHistoryDTO>> queryEditHistoryByFileId(@PathVariable("id") Long fileId, @RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) {
        return Result.success(documentService.queryEditHistoryByFileId(fileId, page, pageSize));
    }


    /**
     * 移动端
     * 查询文件夹以及文件夹包含文档
     * @param folderAndFileDTO
     * @return
     */
    @GetMapping("/document/listFolderAndFile")
    public Result<DocumentDTO> queryFolderAndFile(FolderAndFileDTO folderAndFileDTO){
        return Result.success(documentService.queryFolderAndFile(folderAndFileDTO));
    }

    /**
     * 添加文件夹
     * 移动端
     */
    @PostMapping(value = "document/insertFolder")
    public Result insertFolder(@RequestBody DocFolderDTO folderDTO) throws Exception {
        documentService.save(folderDTO);
        return Result.success();
    }


    /**
     * 添加文件
     * 移动端
     */
    @PostMapping(value = "/document/insertFile")
    public Result insertFile(@RequestBody DocFileDTO fileDTO) throws Exception {
        documentService.insertFile(fileDTO);
        return Result.success();
    }

    /**
     * 通过文档id获得文档
     * 移动端
     */
    @GetMapping(value = "document/getFileByFileId")
    public Result<DocFileDTO> getFileByFileId(@RequestParam Long id) throws Exception {
        return Result.success(documentService.queryDocFileById(id));
    }


    /**
     * 手动保存更新文档
     * 移动端
     */
    @PostMapping(value = "/document/updateFile")
    public Result updateFile(@RequestBody DocFileDTO fileDTO) throws Exception {
        documentService.updateFile(fileDTO);
        return Result.success();
    }

    /**
     * 移动端-编辑同时修改文档内容
     * @param docFileDTO
     * @return
     */
    @PostMapping("/document/updateFileAuto")
    public Result updateFileAuto(@RequestBody DocFileDTO docFileDTO) throws Exception {
        documentService.updateFileAuto(docFileDTO);
        return Result.success();
    }

    /**
     * 移动端
     * 查询指定文档的协作者列表
     * @param page
     * @param pageSize
     * @param fileId
     * @return
     */
    @GetMapping("/document/pagingCollaborations")
    public Result<PageResult<CompanyUserDTO>> pagingCollaborations(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam("id") Long fileId
    ){
        return Result.success(documentService.pagingCollaborations(fileId, page, pageSize));
    }

    /**
     * 远程调用系统微服务获得所有员工记录信息 判断是是否是拥有者或者协作者
     * @param fileId 文档ID
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/document/pagingUsers")
    public Result<List<UserCollaborationDTO>> pagingCollaborationUsers(@RequestParam("id") Long fileId) throws Exception {
        return Result.success(documentService.pagingCollaborationUsers(fileId));
    }

    /**
     * 添加协作者
     * 移动端
     */
    @PostMapping(value = "/document/insertCollaboration")
    public Result insertCollaboration(@RequestBody CollaborationsDTO collaborationsDTO) throws Exception {
        documentService.insertCollaboration(collaborationsDTO);
        return Result.success();
    }

    /**
     * 删除协作者
     * 移动端
     */
    @PostMapping(value = "/document/deleteCollaboration")
    public Result deleteCollaboration(@RequestBody CollaborationsDTO collaborationsDTO) throws Exception {
        documentService.deleteCollaboration(collaborationsDTO);
        return Result.success();
    }

    /**
     * 修改制定文档权限
     * @param fileId
     * @param authority
     * @return
     */
    @PutMapping("/document/updateAuthority/{id}/{authority}")
    public Result updateAuthority(@PathVariable("id") Long fileId, @PathVariable("authority") Integer authority){
        documentService.updateAuthority(fileId, authority);
        return Result.success();
    }
}
